<?php
declare (strict_types=1);

namespace app\exam\logic;

use app\common\exception\LogicException;
use app\common\logic\category\Category;
use app\exam\model\Point as PointModel;
use app\exam\logic\ModuleConfig as ModuleConfigLogic;
use app\exam\logic\Topic;
use app\exam\logic\Option;
use app\exam\logic\admin\ModulePointCates;

/**
 * 知识点逻辑
 */
class Point extends PointModel
{

	/**
	 * 获取知识点列表树
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date 2021-02-03
	 * @param  [type] $param [description]
	 * @return [type]        [description]
	 */
	public function getPointList($param)
	{
		// 验证数据
		$this->getValidate()->scene('point_list')->failException()->check($param);
		// 参数
		$moduleId 	= $param['module_id'];
		$category 	= $param['category']??0;
		if(!$category) {
            /** @var Category $cateLogic */
            $cateLogic = app(Category::class);
            $category = $cateLogic->getFirstTopId(3);
        }

		// 版块名称
		$module_title = app(Module::class)->whereId($moduleId)->value('title');
		// 版块所包含的分类
		$cates = app(ModulePointCates::class)->where('module_id', $moduleId)->findOrEmpty();
		$cates = $cates->isEmpty() ? [] : $cates->getAttr('cates');
		// 
		$field = 'id,pid,title';
		$data = $this->field($field)
			->wherePid(0)
			->whereIn('last_category', $cates)
			->whereLike('category', '%,' . $category . ',%')
			// ->buildSql();halt($data);
			->select()
			->each(function ($item) use ($field) {
				$item->child = $this->field($field)->wherePid($item->id)->select();
			});

		return [
			'module_title' 	=> $module_title,
			'point_tree' 	=> $data
		];
	}

	/**
	 * 获取知识点分类树
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date 2021-02-03
	 * @param  [type] $param [description]
	 * @return [type]        [description]
	 */
	public function getCategoryList($param)
	{
		// 验证数据
		$this->getValidate()->scene('category_list')->failException()->check($param);
		// 参数
		$module_id 	= $param['module_id']??0;
		$value 		= app(ModuleConfigLogic::class)->getValueInfo($module_id);
		$point 		= array_filter($value['point'], function($value){ return count($value) == 2; });
		$point_ids  = [];
		$parent 	= array_unique(array_column($point, 0));

		foreach ( $point as $val ) {
			$point_ids[$val[0]][] = $val[1];
		}

		$field = 'id,pid,title';
		$data  = $this->whereId('IN', $parent)->field($field)->select()
			->each(function ($item) use ($field, $point_ids) {
				$item->child = $this->field($field)->whereId('IN', $point_ids[$item->id])->select();
			})
		;

		return [
			'module_title' 	=> $value['module_title'],
			'point_limit'  	=> $value['limit'],
			'point_tree' 	=> $data
		];
	}

	/**
	 * 获取选择知识点后的试题基本信息
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date 2021-02-03
	 * @param  [type] $param [description]
	 * @return [type]        [description]
	 */
	public function getPracticeBase($param)
	{
		// 数据验证
		$this->getValidate()->scene('practice_base')->failException()->check($param);
		// 参数
		$point_ids	= explode(",", $param['point_ids']);
		$module_id	= $param['module_id'];
		$topic_num	= intval($param['topic_num']);
		//
		$module_title = app(Module::class)->whereId($module_id)->value('title');
		//
		$map = [];
		foreach ($point_ids as $id) {
			$map[] = [
				['question_type', 'IN', '1,2,3,4,7'],
				['point', 'LIKE', '%,'. $id . ',%']
			];
		}

		$field 	= "id,question_type";

		$list 	= [];
		$data 	= app(Topic::class)->withoutGlobalScope()->field($field)->whereOr($map)->limit($topic_num)->select();

		foreach ($data as $val) {
			if ( array_key_exists($val->question_type, $list) ) {
				$list[$val->question_type]['num'] 				+= 1;
				$list[$val->question_type]['data'][$val->id] 	= [
					'topic_id' 		=> $val->id,
					'has_answered'	=> 0
				];
			} else {
				$list[$val->question_type]['title'] 			= $val->question_type_text;
				$list[$val->question_type]['question_type'] 	= $val->question_type;
				$list[$val->question_type]['num'] 				= 1;
				$list[$val->question_type]['data'][$val->id] 	= [
					'topic_id' 		=> $val->id,
					'has_answered'	=> 0
				];
			}
			$list[$val->question_type]['data'] = array_values($list[$val->question_type]['data']);
		}

		return ['module_title' => $module_title, 'topic_num' => count($data), 'answered_num' => 0, 'rules' => array_values($list)];
	}

	/**
	 * 获取试题信息及选项
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date 2021-02-04
	 * @param  [type] $param [description]
	 * @return [type]        [description]
	 */
	public function getPointTopic($param)
	{
		// 数据验证
		$this->getValidate()->scene('point_topic')->failException()->check($param);
		// 参数
		$topic_id	= $param['topic_id'];

		return app(Topic::class)->getTopicInfoById($topic_id);
	}

	/**
	 * 提交知识点练习答案
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date 2021-02-04
	 * @return [type] [description]
	 */
	public function postTopicAnswer($param)
	{
		// 数据验证
		$this->getValidate()->scene('topic_answer')->failException()->check($param);
		// 参数
		$topic_level	= intval($param['topic_level']);
		$topic_id		= intval($param['topic_id']);
		$is_right		= intval($param['is_right']);

		app(TopicPracticeRecord::class)->addRecord($topic_level, $topic_id, $is_right);
	}


}